/*
package com.lh.boot.mybatis.fkfb.config;

import cn.hutool.extra.spring.SpringUtil;
import com.lh.boot.mybatis.fkfb.config.strategy.AbstractSplitTableStrategy;
import com.lh.boot.mybatis.fkfb.config.strategy.StrategyManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.List;
import java.util.Properties;

*/
/**
 * @author: StarrySky
 * @createDate: 2021/8/23 10:05
 * @version: 1.0
 * @description:
 *//*

@Slf4j
@Component
@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public class MyInterceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY,
                SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        // id为执行的mapper方法的全路径名
        String id = mappedStatement.getId();
        BoundSql boundSql = statementHandler.getBoundSql();
        // 注解逻辑判断 添加注解了才拦截
        Class<?> classType = Class.forName(id.substring(0, mappedStatement.getId().lastIndexOf(".")));
        if (classType.isAnnotationPresent(TableSeg.class)) {
            TableSeg tableSeg = classType.getAnnotation(TableSeg.class);
            String sql = rewriteSql(tableSeg, boundSql);
            //通过反射修改sql语句
            Field field = boundSql.getClass().getDeclaredField("sql");
            field.setAccessible(true);
            field.set(boundSql, sql);
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object o) {
        return Plugin.wrap(o, this);
    }

    @Override
    public void setProperties(Properties properties) {
        log.warn("MyInterceptor=======" + properties.toString());
    }

    */
/**
     * 重新sql
     *
     * @param tableSeg 注解
     * @param boundSql sql信息
     * @return 重写后的sql
     *//*

    private String rewriteSql(TableSeg tableSeg, BoundSql boundSql) {
        String sql = boundSql.getSql();
        Object parameterObject = boundSql.getParameterObject();
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        StrategyManager strategyManager = SpringUtil.getBean(StrategyManager.class);
        AbstractSplitTableStrategy strategy = strategyManager.getStrategy(tableSeg.strategy());
        String newTableName = strategy.doSharding(tableSeg.tableName(), parameterMappings, parameterObject);
        String newSql = sql.replaceAll(tableSeg.tableName(), newTableName);
        log.info("rewriteSql=======> logicTable={}", tableSeg.tableName());
        log.info("rewriteSql=======> logicSql={}", sql);
        log.info("rewriteSql=======> newTableName={}", newTableName);
        log.info("rewriteSql=======> newSql={}", newSql);
        return newSql;
    }
}

*/
